<?php

/**
 * This is the model class for table "{{advance_profile}}".
 *
 * The followings are the available columns in table '{{advance_profile}}':
 * @property integer $id
 * @property integer $profile_id
 * @property string $id_card_number
 * @property string $id_card_provided_date
 * @property string $id_card_provided_place
 * @property string $passport_number
 * @property string $passport_provided_date
 * @property string $passport_provided_place
 * @property integer $hiring_company_id
 * @property integer $introducing_company_id
 * @property string $apply_date
 * @property string $selected_date
 * @property string $order
 * @property decimal $contract_duration
 * @property string $go_date
 * @property string $arrive_city
 * @property string $flight
 * @property string $salary
 * @property string $management_fee
 * @property string $introducing_fee
 * @property string $visa_address
 * @property string $contract_number
 * @property string $dispatching_company
 * @property string $url_visa
 * @property string $url_contract
 * @property string $url_id_card_photo
 * @property string $create_date
 * @property string $created_by
 * @property string $update_date
 * @property string $updated_by
 * @property string $back_date_before_contract
 * @property string $code
 * @property string $note
 * The followings are the available model relations:
 * @property Profile $profile
 * @property HiringCompany $hiringCompany
 * @property IntroducingCompany $introducingCompany
 */
class AdvanceProfile extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AdvanceProfile the static model class
	 */
	public $complete_contract_date;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{advance_profile}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('contract_duration, profile_id, hiring_company_id, introducing_company_id', 'numerical', 'integerOnly'=>false, 'message' => t ( 'MSG_WRONG_NUMBER', 'messages' )),
			array('id_card_number, management_fee, introducing_fee, contract_number, salary, id_card_number, passport_number, created_by, updated_by', 'length', 'max'=>45, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('url_id_card_photo, url_visa, url_contract, dispatching_company, arrive_city, flight, order, id_card_provided_place, passport_provided_place', 'length', 'max'=>100, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('code', 'length', 'max'=>20, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('note', 'length', 'max'=>1000, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('visa_address', 'length', 'max'=>200, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('url_id_card_photo', 'file', 'types' => 'JPG,jpg,jpeg,bmp,png,gif', 'maxSize' => 1024 * 1024 * 10, // 10MB
					'tooLarge' => t('MSG_IMAGE_TOO_LARGE', 'messages'),
					'wrongType' => t('MSG_FILE_WRONG_TYPE', 'messages'),
					'allowEmpty' => true, 'on' => array('create','update')),
			array('url_contract', 'file', 'types' => 'JPG,jpg,jpeg,bmp,png,gif', 'maxSize' => 1024 * 1024 * 10, // 10MB
					'tooLarge' => t('MSG_IMAGE_TOO_LARGE', 'messages'),
					'wrongType' => t('MSG_FILE_WRONG_TYPE', 'messages'),
					'allowEmpty' => true, 'on' => array('create','update')),
			array('url_visa', 'file', 'types' => 'JPG,jpg,jpeg,bmp,png,gif', 'maxSize' => 1024 * 1024 * 10, // 10MB
					'tooLarge' => t('MSG_IMAGE_TOO_LARGE', 'messages'),
					'wrongType' => t('MSG_FILE_WRONG_TYPE', 'messages'),
					'allowEmpty' => true, 'on' => array('create','update')),
			array('back_date_before_contract, url_id_card_photo, complete_contract_date, go_date, apply_date, selected_date, id_card_provided_date, passport_provided_date, create_date, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, profile_id, id_card_number, back_date_before_contract, id_card_provided_date, id_card_provided_place, passport_number, passport_provided_date, passport_provided_place, hiring_company_id, introducing_company_id, create_date, created_by, update_date, updated_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'profile' => array(self::BELONGS_TO, 'Profile', 'profile_id'),
			'hiringCompany' => array(self::BELONGS_TO, 'HiringCompany', 'hiring_company_id'),
			'introducingCompany' => array(self::BELONGS_TO, 'IntroducingCompany', 'introducing_company_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('LBL_ID','advanceProfile'),
			'profile_id' => t('LBL_PROFILE_ID','advanceProfile'),
			'id_card_number' => t('LBL_ID_CARD_NUMBER','advanceProfile'),
			'id_card_provided_date' => t('LBL_ID_CARD_PROVIDED_DATE','advanceProfile'),
			'id_card_provided_place' => t('LBL_ID_CARD_PROVIDED_PLACE','advanceProfile'),
			'passport_number' => t('LBL_PASSPORT_NUMBER','advanceProfile'),
			'passport_provided_date' => t('LBL_PASSPORT_PROVIDED_DATE','advanceProfile'),
			'passport_provided_place' => t('LBL_PASSPORT_PROVIDED_PLACE','advanceProfile'),
			'hiring_company_id' => t('LBL_HIRING_COMPANY_ID','advanceProfile'),
			'introducing_company_id' => t('LBL_INTRODUCING_COMPANY_ID','advanceProfile'),
			'create_date' => t('LBL_CREATE_DATE','advanceProfile'),
			'created_by' => t('LBL_CREATED_BY','advanceProfile'),
			'update_date' => t('LBL_UPDATE_DATE','advanceProfile'),
			'updated_by' => t('LBL_UPDATED_BY','advanceProfile'),
			'apply_date' => t('LBL_APPLY_DATE','advanceProfile'),
			'selected_date' => t('LBL_SELECTED_DATE','advanceProfile'),
			'order' => t('LBL_ORDER','advanceProfile'),
			'contract_duration' => t('LBL_CONTRACT_DURATION','advanceProfile'),
			'go_date' => t('LBL_GO_DATE','advanceProfile'),
			'arrive_city' => t('LBL_ARRIVE_CITY','advanceProfile'),
			'flight' => t('LBL_FLIGHT','advanceProfile'),
			'salary' => t('LBL_SALARY','advanceProfile'),
			'management_fee' => t('LBL_MANAGEMENT_FEE', 'advanceProfile'),
			'introducing_fee' => t('LBL_INTRODUCING_FEE', 'advanceProfile'),
			'complete_contract_date' => t('LBL_COMPLETE_CONTRACT_DATE','advanceProfile'),
			'visa_address' => t('LBL_VISA_ADDRESS','advanceProfile'),
			'contract_number' => t('LBL_CONTRACT_NUMBER','advanceProfile'),
			'dispatching_company' => t('LBL_DISPATCHING_COMPANY','advanceProfile'),
			'url_visa' => t('LBL_URL_VISA','advanceProfile'),
			'url_contract' => t('LBL_URL_CONTRACT','advanceProfile'),
			'url_id_card_photo' => t('LBL_URL_ID_CARD_PHOTO','advanceProfile'),
			'code' => t('LBL_CODE', 'advanceProfile'),
			'note' => t('LBL_NOTE', 'advanceProfile'),
			'back_date_before_contract' => t('LBL_BACK_DATE_BEFORE_CONTRACT', 'advanceProfile')
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('profile_id',$this->profile_id);
		$criteria->compare('id_card_number',$this->id_card_number,true);
		$criteria->compare('id_card_provided_date',$this->id_card_provided_date,true);
		$criteria->compare('id_card_provided_place',$this->id_card_provided_place,true);
		$criteria->compare('passport_number',$this->passport_number,true);
		$criteria->compare('passport_provided_date',$this->passport_provided_date,true);
		$criteria->compare('passport_provided_place',$this->passport_provided_place,true);
		$criteria->compare('hiring_company_id',$this->hiring_company_id);
		$criteria->compare('introducing_company_id',$this->introducing_company_id);
		$criteria->compare('salary',$this->introducing_company_id);
		$criteria->compare('back_date_before_contract',$this->back_date_before_contract,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('update_date',$this->update_date,true);
		$criteria->compare('updated_by',$this->updated_by,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave() {
		if (parent::beforeSave()) {
	
			$current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);
	
			//Get user is logging
			$logedInUser = User::model()->findByPk(Yii::app()->user->id);
			
			$this->update_date = $current_date;
			
			if(isset($this->id_card_provided_date) && $this->id_card_provided_date != NULL){
				$date = DateTime::createFromFormat ( 'd-m-Y', $this->id_card_provided_date );
				$this->id_card_provided_date = $date->format ( 'Y-m-d' );
			}else{
				$this->id_card_provided_date = NULL;
			}
			
			if(isset($this->passport_provided_date) && $this->passport_provided_date != NULL){
				$date = DateTime::createFromFormat ( 'd-m-Y', $this->passport_provided_date );
				$this->passport_provided_date = $date->format ( 'Y-m-d' );
			}else{
				$this->passport_provided_date = NULL;
			}
			
			if(isset($this->go_date) && $this->go_date != NULL){
				$date = DateTime::createFromFormat ( 'd-m-Y', $this->go_date );
				$this->go_date = $date->format ( 'Y-m-d' );
			}else{
				$this->go_date = NULL;
			}
			
			if(isset($this->apply_date) && $this->apply_date != NULL){
				$date = DateTime::createFromFormat ( 'd-m-Y', $this->apply_date );
				$this->apply_date = $date->format ( 'Y-m-d' );
			}else{
				$this->apply_date = NULL;
			}
			
			if(isset($this->selected_date) && $this->selected_date != NULL){
				$date = DateTime::createFromFormat ( 'd-m-Y', $this->selected_date );
				$this->selected_date = $date->format ( 'Y-m-d' );
			}else{
				$this->selected_date = NULL;
			}
			
			if(isset($this->back_date_before_contract) && $this->back_date_before_contract != NULL){
				$date = DateTime::createFromFormat ( 'd-m-Y', $this->back_date_before_contract );
				$this->back_date_before_contract = $date->format ( 'Y-m-d' );
			}else{
				$this->back_date_before_contract = NULL;
			}

			if(!isset($this->contract_duration) || $this->contract_duration == ''){
				$this -> contract_duration = NULL;
			}
	
			if (isset($logedInUser)) {
				$this->updated_by = $logedInUser->username;
				if ($this->isNewRecord) {
					$this->create_date = $current_date;
					$this->created_by = $logedInUser->username;
				}
			}
	
			$this->setIsNewRecord(false);
	
			return true;
		} else {
			return false;
		}
	}
	
	protected function afterFind() {
		if($this->id_card_provided_date != NULL){
			$this->id_card_provided_date = strtotime ( $this->id_card_provided_date );
			$this->id_card_provided_date = date ( LGConstant::DATE_FORMAT_NOT_TIME_MONTH_NUMBER, $this->id_card_provided_date );
		}
		
		if($this->passport_provided_date != NULL){
			$this->passport_provided_date = strtotime ( $this->passport_provided_date );
			$this->passport_provided_date = date ( LGConstant::DATE_FORMAT_NOT_TIME_MONTH_NUMBER, $this->passport_provided_date );
		}
		
		if($this->apply_date != NULL){
			$this->apply_date = strtotime ( $this->apply_date );
			$this->apply_date = date ( LGConstant::DATE_FORMAT_NOT_TIME_MONTH_NUMBER, $this->apply_date );
		}
		
		if($this->selected_date != NULL){
			$this->selected_date = strtotime ( $this->selected_date );
			$this->selected_date = date ( LGConstant::DATE_FORMAT_NOT_TIME_MONTH_NUMBER, $this->selected_date );
		}
		
		if($this->back_date_before_contract != NULL){
			$this->back_date_before_contract = strtotime ( $this->back_date_before_contract );
			$this->back_date_before_contract = date ( LGConstant::DATE_FORMAT_NOT_TIME_MONTH_NUMBER, $this->back_date_before_contract );
		}
		
		if($this->go_date != NULL){
			$this->go_date = strtotime ( $this->go_date );
			$this->go_date = date ( LGConstant::DATE_FORMAT_NOT_TIME_MONTH_NUMBER, $this->go_date );
			if($this->contract_duration != NULL)
				$this->complete_contract_date = date('d-m-Y', strtotime("+" . ($this->contract_duration * 12) . " months", strtotime($this->go_date)));
		}
	
		parent::afterFind ();
	}
}